/*格式化小于10的数字*/
let formatTen = function (no) {
  let num = Number(no); //转换数字
  if (num < 10) {
      return '0' + num;
  }
  return num;
};
//格式化日期
let formatDate = function (date) {
  let d = new Date(date);
  return date.getFullYear() + "-" + formatTen(date.getMonth() + 1) + "-" + formatTen(date.getDate()) + " " + formatTen(date.getHours()) + ":" + formatTen(date.getMinutes()) + ":" + formatTen(date.getSeconds());
};

//计算Object或者string的长度
let count = function(o) {
  let t = typeof o;
  if ('string' == t) {
      return o.length;
  } else if ('object' == t) {
      let n = 0;
      for (let i in o) {
          n++;
      }
      return n;
  }
  return false;
}

//深拷贝对象
let deepCopyObj = function(p, c) {
  var c = c || {};
　for (var i in p) {
  　if (typeof p[i] === 'object' && p[i] != null) {
      c[i] = (p[i].constructor === Array) ? [] : {};
      deepCopyObj(p[i], c[i]);
  　} else {
  　　c[i] = p[i];
    }
  }
　return c;
}

// 深拷贝对象数组
let deepCopyObjArray = function (arr) {
  arr = arr && arr.length > 0 ? arr : [];
  arr = arr.constructor == Array ? arr : [];
  return [].concat(JSON.parse(JSON.stringify(arr)));
}

//把JSON对象转成GET方法的字符串参数
let jsonToUrl = function (obj) {
  let result = '';
  for(let key in obj) {
    result += '&' + key + '=' + obj[key];
  }
  result = '?' + result.substring(1, result.length);
  return result;
}

let imgError = function(e, flag) {
  let src = '';
  switch(flag) {
    case 'car' : src = '@/assets/img/car.png';break;
    case 'license' : src = '@/assets/img/license.png';break;
    default: src = '@/assets/img/car.png';break;
  }
  e.src = src;
}

let getByteLength = function (str) {
  let realLength = 0;
  let len = str.length;
  let charCode = -1;
  for (var i = 0; i < len; i++) {
    charCode = str.charCodeAt(i);
    if (charCode >= 0 && charCode <= 128) realLength += 1;
    else realLength += 2;
  }
  return realLength;
}

let mileSecondToDate = function(value) {
  let result = '';
  if(value) {
    let date = new Date(value);
    result =  date.getFullYear() + "-" + formatTen(date.getMonth() + 1) + "-" + formatTen(date.getDate()) + " " + formatTen(date.getHours()) + ":" + formatTen(date.getMinutes()) + ":" + formatTen(date.getSeconds());
  }
  return result;
}

let langObj = JSON.parse(localStorage.getItem('langObj'));
let _day =  '天';
let _hour = '时';
let _minute = '分';
if(langObj) {
  _day =  langObj.normal_label_time_day || '天';
  _hour = langObj.normal_label_time_hour || '时';
  _minute = langObj.normal_label_time_minute || '分';
}

let minutesToHowLong = function(Val) {
  let mits = Val < 0 ? 0 : Val;
  //计算天数
  let days = Math.floor(mits / (60 * 24));

  //计算小时数
  let leave_d = mits % (60 * 24); //计算除去整天数后还剩下的分钟数
  let hours = Math.floor(leave_d / 60);

  //计算分钟数
  let minutes = leave_d % 60;
  
  if(days !== 0) {
    return '' + days + _day + hours + _hour + minutes + _minute;
  } else if (hours !== 0) {
    return '' + hours + _hour + minutes + _minute;
  } else {
    return '' + minutes + _minute;
  }
}

// 把对象中的每一个属性的值都置空
let clearObject = function(obj) {
  for(let key in obj ){
    obj[key] = '';
  }
  return obj;
}

//根据目标对象中存在的属性，把原始对象中的数据进行拷贝，（PS:目的是可以去除对象中的无用属性）
let copyObjBySelf = function (target, origin) {
  for(let key in target) {
    target[key] = origin[key];
  }
  return target;
}

// float型计算

//乘    
function floatMul(arg1,arg2)   {     
  var m=0,s1=arg1.toString(),s2=arg2.toString();     
  try{m+=s1.split(".")[1].length}catch(e){}     
  try{m+=s2.split(".")[1].length}catch(e){}     
  return Number(s1.replace(".",""))*Number(s2.replace(".",""))/Math.pow(10,m);     
}     
    
    
//除   
function floatDiv(arg1,arg2){     
  if(arg1%arg2===0){ //可整除
    return Math.round(arg1/arg2)
  }
  var t1=0,t2=0,r1,r2;     
  try{t1=arg1.toString().split(".")[1].length}catch(e){}     
  try{t2=arg2.toString().split(".")[1].length}catch(e){}     
    
  r1=Number(arg1.toString().replace(".",""));  
  r2=Number(arg2.toString().replace(".",""));   
  
  var rs = Math.floor((r1/r2)*Math.pow(10,t2-t1)*100)/100
  if(rs){
    rs = (rs+0.01).toFixed(2)
  }
  return rs;   
} 

// 获取停车场树叶子节点
function getTreeChildNodes(nodes,nameStr){
  if(!nodes || !nodes.length){
    return [];
  }else{
    let arr = []
    for(let i = 0; i<nodes.length; i++){
      if(nodes[i].isParent && nodes[i].children){
        let name = nodes[i].name.replace(/\(.*?\)/g, '')
        name = (nameStr ? nameStr+'-' : '') + name
        let tmpArr = getTreeChildNodes(nodes[i].children,name)
        arr = tmpArr.concat(arr)
      }else{
        nodes[i].title = nameStr
        if(nameStr){
          nodes[i].topArea = adjustProvinceNameOfParkingLot(nameStr.substring(0,nameStr.indexOf('-')))
        }else{
          nodes[i].topArea = ''
        }
        arr.push(nodes[i])
      }
    }
    return arr;
  }
}

// 调整停车场所属省份名
function adjustProvinceNameOfParkingLot(provinceName){
  if(!provinceName){
    return provinceName
  }
  let idx = 0
  let rsStr = ''
  provinceName = provinceName.replace(/\(.*?\)/g, '')
  if(provinceName.indexOf('省')>-1){
    idx = provinceName.indexOf('省')
  }else if(provinceName.indexOf('自治区')>-1){
    idx = provinceName.indexOf('自治区')
  }else if(provinceName.indexOf('市')>-1){
    idx = provinceName.indexOf('市')  
  }else if(provinceName.length > 6){
    idx = 1
  }
  if(idx!==0){
    rsStr = provinceName.substring(0,idx)
    return rsStr
  }else{
    return provinceName
  }
}

export {
  getTreeChildNodes,
  adjustProvinceNameOfParkingLot,
  formatTen,
  formatDate,
  count,
  deepCopyObj,
  deepCopyObjArray,
  imgError,
  jsonToUrl,
  getByteLength,
  mileSecondToDate,
  minutesToHowLong,
  clearObject,
  copyObjBySelf,
  floatDiv,
  floatMul
}
